package com.mjh.zhujianju.service.set.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.livew.framework.base.service.impl.BaseServiceImpl;
import com.livew.framework.enums.EStatus;
import com.livew.framework.exception.XTBusinessException;
import com.livew.framework.page.PagedBeanUtil;
import com.livew.framework.page.PagedResult;
import com.livew.framework.sql.XTSqlUtils;
import com.livew.framework.utils.XTListUtils;
import com.livew.framework.utils.XTStringUtils;
import com.mjh.zhujianju.dao.set.AnnouncementMapper;
import com.mjh.zhujianju.enums.BusinessEnum;
import com.mjh.zhujianju.model.base.ContentEO;
import com.mjh.zhujianju.model.set.AnnouncementEO;
import com.mjh.zhujianju.service.base.ContentService;
import com.mjh.zhujianju.service.base.HitsService;
import com.mjh.zhujianju.service.set.AnnouncementService;
import com.mjh.zhujianju.vo.set.AnnouncementVO;

/**
 * @author liuyang
 * @Description: 公告
 * @date 2018/8/8 22:19
 */
@Service
public class AnnouncementServiceImpl extends BaseServiceImpl<AnnouncementEO> implements AnnouncementService {

    @Autowired
    private AnnouncementMapper announcementMapper;

    @Autowired
    private ContentService contentService;

    @Autowired
    private HitsService hitsService;

    public AnnouncementServiceImpl(AnnouncementMapper announcementMapper) {
        super(announcementMapper);
    }

    @Override
    public List<AnnouncementEO> queryAnnouncementNewOne() {
    	SimpleDateFormat sdf1 = new SimpleDateFormat("MM-dd");
		List<AnnouncementEO> list = announcementMapper.queryAnnouncementNewOne();
		for(AnnouncementEO eo : list) {
			String str = sdf1.format(eo.getCreateTime());
			eo.setRemark2(str);
		}
        return list;
    }

    @Override
    public List<AnnouncementVO> queryAnnouncementList() {
        return announcementMapper.queryAnnouncementList();
    }

    @Override
    public AnnouncementVO queryAnnouncementInfo(Long id) {
        AnnouncementVO announcementVO = new AnnouncementVO();
        AnnouncementEO announcementEO = announcementMapper.queryAnnouncementInfo(id);
        if (announcementEO == null){
            return announcementVO;
        }
        BeanUtils.copyProperties(announcementEO,announcementVO);

        String type = BusinessEnum.TYPE_ANNOUNCEMENT.getCode();

        //添加浏览量
        hitsService.updateNumber(type,id);

        //内容
        ContentEO contentEO = contentService.queryContentByTypeAndId(type,id);
        if (contentEO != null){
            announcementVO.setContent(contentEO.getContent());
        }

        //阅读数量
        Long hitsTotal = hitsService.queryHitsTotal(type,id);
        announcementVO.setTotalHits(hitsTotal);

        return announcementVO;
    }

    @Override
  	public void saveInfo(AnnouncementVO vo) throws XTBusinessException {
  		AnnouncementEO eo = new AnnouncementEO();
  		BeanUtils.copyProperties(vo, eo);
  		eo.setStatus(EStatus.active.getCode());
  		this.save(eo);
  		contentService.saveOrUpdate(BusinessEnum.TYPE_ANNOUNCEMENT.getCode(), eo.getId(), vo.getContentEO().getContent());
  	}

  	@Override
  	public void updateInfo(AnnouncementVO vo) throws XTBusinessException {
  		AnnouncementEO eo = new AnnouncementEO();
  		BeanUtils.copyProperties(vo, eo);
  		eo.setStatus(EStatus.active.getCode());
  		this.updateByPrimaryKeySelective(eo);
  		contentService.saveOrUpdate(BusinessEnum.TYPE_ANNOUNCEMENT.getCode(), eo.getId(), vo.getContentEO().getContent());
  	}

  	@Override
  	public PagedResult<AnnouncementVO> getVOPage(AnnouncementVO vo) {
  		Integer pageNo = vo.getPageNo() == null ? 1 : vo.getPageNo();
  		Integer pageSize = vo.getPageSize() == null ? 15 : vo.getPageSize();
  		PageHelper.startPage(pageNo, pageSize);
  		if(!XTStringUtils.isEmpty(vo.getTitle())) {
  			vo.setTitle(XTSqlUtils.replaceSeparator(vo.getTitle()));
  		}
  		List<AnnouncementEO> list = announcementMapper.selectPagination(vo);
  		PagedResult<AnnouncementEO> page = PagedBeanUtil.toPagedResult(list);
  		PagedResult<AnnouncementVO> result = new PagedResult<>();
  		BeanUtils.copyProperties(page, result);
  		if (!XTListUtils.isEmpty(list)) {
//  			List<Long> ids = XTEntityUtils.getIdList(list);
  			//工作内容
  			List<AnnouncementVO> voList = new ArrayList<AnnouncementVO>();
			for (AnnouncementEO AnnouncementEO : list) {
				AnnouncementVO AnnouncementVO =new AnnouncementVO();
				BeanUtils.copyProperties(AnnouncementEO, AnnouncementVO);
//				AnnouncementVO.setSiteName(BanersEnum.getName(AnnouncementVO.getSite()));
				voList.add(AnnouncementVO);
  			}
  			result.setDataList(voList);
  		}
  		return result;
  	}

  	@Override
  	public AnnouncementVO getInfo(Long id) throws XTBusinessException{
  		AnnouncementEO wd = super.getByPrimaryKey(id);
  		if (wd != null) {
  			AnnouncementVO vo = new AnnouncementVO();
  			BeanUtils.copyProperties(wd, vo);
//  			vo.setSiteName(BanersEnum.getName(vo.getSite()));
  		ContentEO content =	contentService.queryContentByTypeAndId(BusinessEnum.TYPE_ANNOUNCEMENT.getCode(), vo.getId());
  		vo.setContentEO(content);
  		return vo;
  		}

  		return null;
  	}
  
}
